package org.millioncall.yueyoga.admin.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.NumberUtils;
import org.millioncall.yueyoga.admin.dao.ConsumptionDAO;
import org.millioncall.yueyoga.admin.dao.MemberDAO;
import org.millioncall.yueyoga.admin.dao.ProductRemainingDAO;
import org.millioncall.yueyoga.admin.dao.RemainingDAO;
import org.millioncall.yueyoga.admin.model.Consume;
import org.millioncall.yueyoga.admin.model.Consumption;
import org.millioncall.yueyoga.admin.model.Member;
import org.millioncall.yueyoga.admin.model.ProductRemaining;
import org.millioncall.yueyoga.admin.model.Remaining;
import org.millioncall.yueyoga.admin.service.ConsumptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ConsumptionServiceImpl implements ConsumptionService {

	@Autowired
	private ConsumptionDAO consumptionDAO;
	@Autowired
	private ProductRemainingDAO productRemainingDAO;
	@Autowired
	private RemainingDAO remainingDAO;
	@Autowired
	private MemberDAO memberDAO;
	
	Map<String, Object> map = null;

	@Override
	public boolean saveConsumption(Consumption consumption) {
		map = new HashMap<String, Object>();
		map.put("consumptionId", consumption.getConsumptionid());
		map.put("inputTime", consumption.getInputTime());
		map.put("memberId", consumption.getMember().getMemberid());
		map.put("productId", consumption.getProduct().getProductId());
		return consumptionDAO.saveConsumption(map);
	}

	@Override
	public Consumption findConsumptionById(Integer id) {
		if (id != null) {
			return consumptionDAO.findConsumptionById(id);
		}
		return null;
	}

	@Override
	public PaginatedList<Consumption> findConsumptionsInPage(
			Map<String, Object> map, int start, int limit) {
		PaginatedList<Consumption> pl =  consumptionDAO.findConsumptionsInPage(map, start, limit);
		return pl;
	}

	@Override
	public boolean saveConsume(Consume consume) {
		return this.consumptionDAO.saveConsume(consume);
	}

	@Override
	public String saveConsume(Consume consume, ProductRemaining pr, Member mem) {
		String msg = "true";
		boolean b = this.consumptionDAO.saveConsume(consume);
		if (!b) {
			msg = "{'success':false, 'msg':'添加消费记录失败！'}";
			return msg;
		}
		//次卡
		if (101 == pr.getProduct().getType()) {
			//获得剩余产品的可用次数
			int count = pr.getCounts();
			if (count <= 0) {
				msg = "{'success':false,'msg':'次卡次数已不足!'}";
				return msg;
			}
			count--;
			int flag = productRemainingDAO.updateCounts(pr.getId(), count);
			if (flag <= 0) {
				msg = "{'success':true,	'msg':'次卡数量更新失败!'}";
				return msg;
			}
		} else if (107 == pr.getProduct().getType() || 108 == pr.getProduct().getType()) { // 此状态单独消费该产品类型
			Remaining remaining = remainingDAO.findById(mem.getMemberid());
			Double money = remaining.getRemaining();
			Double proPrice = pr.getProduct().getPrice();
			// 金额计算
			money = NumberUtils.sub(money, proPrice);
			if (money < 0) {
				msg = "{'success':false,'msg':'您的金额已不足,请及时充值!'}";
				return msg;
			} else {
				remaining.setRemaining(money);
				remainingDAO.updateRemainingById(remaining);
			}
		} else {
			Date date = new Date();
			if (pr.getEndTime() == null) {
				
			} else {
				if (date.after(pr.getEndTime())) {
					msg = "{'success':false,'msg':'产品已过期!'}";
					return msg;
				}
			}
		}
		//修改会员状态
		if ("0021".equals(mem.getSubstatus())) {
			
		} else {
			mem.setSubstatus("0021");
			if (memberDAO.update(mem) > 0) {
				
			} else {
				msg = "{'success': 'false', 'msg': '更新会员状态失败!'}";
				return msg;
			}
		}
		return msg;
	}

	@Override
	public boolean isConsumed(Map<String, Object> map) {
		return this.consumptionDAO.isConsumed(map);
	}
}
